package per.dg.basicuseretrofit

import android.os.Bundle
import android.util.ArrayMap
import android.util.Log
import android.view.View
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.JsonObject
import okhttp3.ResponseBody
import org.json.JSONObject
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.FieldMap


class MainActivity : AppCompatActivity() {

    private val TAG = "MainActivity"

    val baseurl = "https://www.wanandroid.com"

    val username = "bin"
    val password = "123456"
    val repassword = "123456"

    val userMap : Map<String, String> = mapOf("loginUserName" to username, "loginUserPassword" to password)

    lateinit var cookieList : List<String>

    lateinit var mAPIService: APIService

    private lateinit var btRegist: Button
    private lateinit var btLogin: Button
    private lateinit var btGetHomeArticleList: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btRegist = findViewById(R.id.bt_regist)
        btLogin = findViewById(R.id.bt_login)
        btGetHomeArticleList = findViewById(R.id.bt_get_home_article_list)

        val retrofit: Retrofit = Retrofit.Builder()
                .baseUrl(baseurl)
                .addConverterFactory(GsonConverterFactory.create())
                .build()

        mAPIService = retrofit.create(APIService::class.java)
    }

    private fun regist(){
        var call = mAPIService.regist(username, password, repassword)
        call.enqueue(object : Callback<WanAndroidResponse<WanAndroidUser>> {
            override fun onFailure(call: Call<WanAndroidResponse<WanAndroidUser>>, t: Throwable) {
                Log.e(TAG, "regist onFailure: \n${t.toString()}\n${call.toString()}" )
            }

            override fun onResponse(
                call: Call<WanAndroidResponse<WanAndroidUser>>,
                response: Response<WanAndroidResponse<WanAndroidUser>>
            ) {
                Log.e(TAG, "regist onResponse: ${response.body()}" )
                Log.e(TAG, "onResponse: ${call.request().url().toString()}" )
//                if(response.body()!!.errorMsg.contains("用户名已经被注册")){
//                    login()
//                }
            }

        })
    }

    private fun login(){
        mAPIService.login(username, password).also {
            it.enqueue(object : Callback<WanAndroidResponse<WanAndroidUser>> {
                override fun onFailure(call: Call<WanAndroidResponse<WanAndroidUser>>, t: Throwable) {
                    Log.e(TAG, "login onFailure:\n ${t.toString()}\n${call.toString()}" )
                }

                override fun onResponse(
                    call: Call<WanAndroidResponse<WanAndroidUser>>,
                    response: Response<WanAndroidResponse<WanAndroidUser>>
                ) {
                    Log.e(TAG, "login onResponse: ${response.body().toString()}" )
                    Log.e(TAG, "onResponse: ${response.headers()}" )

                    cookieList = response.headers().values("Set-Cookie")
                    Log.e(TAG, "onResponse: ${response.headers().values("Set-Cookie")}\n${cookieList.size}" )
                }

            })
        }
    }

    private fun getHomeArticleList(){
        mAPIService.homeArticleList(0).also {
            it.enqueue(object : Callback<WanAndroidResponse<ArticleList>> {
                override fun onFailure(
                    call: Call<WanAndroidResponse<ArticleList>>?,
                    t: Throwable?
                ) {
                    Log.e(TAG, "getHomeArticleList onFailure:\n ${t.toString()}\n${call.toString()}" )
                }

                override fun onResponse(
                    call: Call<WanAndroidResponse<ArticleList>>?,
                    response: Response<WanAndroidResponse<ArticleList>>?
                ) {
                    Log.e(TAG, "getHomeArticleList onResponse: ${response!!.body().toString()}" )
                }

            })
        }
    }

    private fun getCollectionArticleList(){

        mAPIService.collectionArticleList(0, mapOf("cookie" to cookieList.toString())).also {
            it.enqueue(object : Callback<WanAndroidResponse<ArticleList>> {
                override fun onFailure(call: Call<WanAndroidResponse<ArticleList>>, t: Throwable) {
                    Log.e(TAG, "getCollectionArticleList onFailure:\n ${t.toString()}\n${call.toString()}" )
                }

                override fun onResponse(
                    call: Call<WanAndroidResponse<ArticleList>>,
                    response: Response<WanAndroidResponse<ArticleList>>
                ) {
                    Log.e(TAG, "getCollectionArticleList onResponse: ${response!!.body().toString()}" )
                }

            })
        }
    }

    private fun collctArticle(){
        mAPIService.collectArticle(18727, cookieList.toString()).also {
            it.enqueue(object : Callback<JsonObject> {
                override fun onFailure(call: Call<JsonObject>, t: Throwable) {
                    Log.e(TAG, "collctArticle onFailure:\n ${t.toString()}\n${call.toString()}" )
                }

                override fun onResponse(call: Call<JsonObject>, response: Response<JsonObject>) {
                    Log.e(TAG, "collctArticle onResponse: ${response!!.body().toString()}" )
                }

            })
        }
    }

    fun onClick(view: View) {
        when(view.id){
            R.id.bt_regist ->{
                regist()
            }
            R.id.bt_login ->{
                login()
            }
            R.id.bt_get_home_article_list ->{
                getHomeArticleList()
            }
            R.id.bt_get_collection_article_list ->{
                getCollectionArticleList()
            }
            R.id.bt_collect_article ->{
                collctArticle()
            }
        }
    }
}